<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Exam</title>
</head>
<body>
    <div class="container" id="exam-content">
        <h1>考试进行中...</h1>
        <p>请不要切换窗口，否则将记录为作弊行为。</p>
    </div>

    <script>
        let focusLostCount = 0;
        const maxCheatAttempts = 2;

        window.onblur = function() {
            focusLostCount++;
            if (focusLostCount > maxCheatAttempts) {
                alert("检测到你多次切换窗口，将记录可能的作弊行为！");
                recordCheating();
            }
        };

        window.onfocus = function() {
            // 恢复焦点时可以执行的操作
            console.log("窗口重新获得焦点");
        };

        function recordCheating() {
            // 使用 Fetch API 向服务器发送作弊行为的请求
            fetch('/record_cheating', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({cheatCount: focusLostCount}),
            })
            .then(response => response.json())
            .then(data => {
                console.log('Success:', data);
                if (data.terminate) {
                    alert("你已达到最大作弊次数，考试将被终止。");
                    window.location.href = '/terminate_exam';
                }
            })
            .catch((error) => {
                console.error('Error:', error);
            });
        }
    </script>
</body>
</html>
